#include "include/vsprintf.h"
#include "include/type.h"
#include "include/klib.h"
int vsprintf(char *buf, const char *fmt, va_list args)
{
    char *p;
    char temp[256];
    va_list p_next_arg = args;
    for (p = buf; *fmt; fmt++)
    {
        if (*fmt != '%')
        {
            *p++ = *fmt;
            continue;
        }
        fmt++;
        switch (*fmt)
        {
        case 'd':
            itoa(temp, *((u32*)p_next_arg),10);
            strcpy(temp, p);
            p_next_arg += 4;
            p += strlen(temp);
            break;
        case 'x':
            itoa(temp, *((u32*)p_next_arg),16);
            strcpy(temp, p);
            p_next_arg += 4;
            p += strlen(temp);
            break;
        case 'c':
			*p++ = *((char*)p_next_arg);
			p_next_arg += 4;
			break;
        case 's':
            strcpy(*((char **)p_next_arg), p);
            p += strlen(*((char **)p_next_arg));
            p_next_arg += 4;
            break;
        default:
            break;
        }
    }
    *p = '\0';
    return (p-buf);
}


int sprintf(char *buf, const char *fmt, ...)
{
    int i;
    va_list arg = (va_list)((char*)(&fmt)+4);
    i = vsprintf(buf, fmt, arg);
    return i;
}